<template>
  <el-dialog
    :title="dialogObj.title"
    append-to-body
    :visible.sync="dialogObj.show"
    :close-on-click-modal="false"
    width="800px"
  >
    <div class="filter-area">
      <div class="operate-area">
        <el-button @click="add" plain type="success" size="mini"
          >添加库存</el-button
        >
      </div>
    </div>
    <el-table
      :data="tableData"
      style="width: 100%"
      border
      size="mini"
      v-if="hackReset"
    >
      <el-table-column type="index" width="50" align="center">
      </el-table-column>
      <el-table-column prop="salesDate" label="日期" align="center">
      </el-table-column>
      <el-table-column prop="totalStock" label="总库存(个)" align="center">
      </el-table-column>
      <el-table-column prop="residueStock" label="剩余库存(个)" align="center">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="primary"
            style="margin: 5px"
            plain
            size="mini"
            @click="mangeInventory(scope.row)"
            >管理库存</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <page
      :nowPage="nowPage"
      :total="total"
      :limit="limit"
      @pageChange="onPageChange"
      @sizeChange="onSizeChange"
    />

    <el-dialog
      width="600px"
      title="添加库存"
      :visible.sync="innerVisible"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        status-icon
        label-width="120px"
        v-if="innerVisible"
        :rules="rules"
      >
        <el-form-item label="日期：" prop="salesDate">
          <el-date-picker
            v-model="form.salesDate"
            type="date"
            size="mini"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="库存量：" prop="stock">
          <el-input
            v-model="form.stock"
            style="width:150px;"
            size="mini"
            :placeholder="placeholderTips.content"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="sub" size="mini">确 定</el-button>
        <el-button @click="innerVisible = false" size="mini">取 消</el-button>
      </span>
    </el-dialog>
  </el-dialog>
</template>
<script>
import { _debounce } from "@u/debounce";
import { placeholderTips, validateNumInt } from "@u/validate";
import page from "@c/page";
import timeFormat from "@u/timeFormat";
import { foodstockListApi, editStocktApi } from "@s/api";
export default {
  name: "dialogCom",
  props: ["dialogObj"],
  data() {
    return {
      nowPage: 1,
      total: 0,
      limit: 10,
      placeholderTips: placeholderTips,
      hackReset: true,
      tableData: [],
      innerVisible: false,
      form: {
        salesDate: "",
        stock: ""
      },
      rules: {
        salesDate: [{ required: true, message: "请选择时间", trigger: "blur" }],
        stock: [
          { required: true, message: "请输入库存", trigger: "blur" },
          { validator: validateNumInt }
        ]
      }
    };
  },
  components: {
    page
  },
  methods: {
    onPageChange(val) {
      this.nowPage = val;
      this.initDialog();
    },
    onSizeChange(val) {
      this.nowPage = 1;
      this.limit = val;
      this.initDialog();
    },
    initDialog() {
      let json = {
        productId: this.dialogObj.id,
        limit: this.limit,
        page: this.nowPage
      };
      foodstockListApi(json).then(res => {
        this.hackReset = false;
        this.$nextTick(() => {
          this.tableData = res.list;
          this.total = res.total;
          this.hackReset = true;
        });
      });
    },
    add() {
      this.innerVisible = true;
      this.form.salesDate = "";
      this.form.stock = "";
    },
    sub: _debounce(function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let salesDate = timeFormat.currentTime(this.form.salesDate);
          let json = {
            productId: this.dialogObj.id,
            salesDate: salesDate,
            stock: this.form.stock,
            operation: 1 // 1 新增
          };
          editStocktApi(json).then(() => {
            this.innerVisible = false;
            this.$message({
              type: "success",
              message: "库存添加成功"
            });
            this.initDialog();
          });
        }
      });
    }),
    // 操作库存
    mangeInventory(val) {
      this.$prompt("正数为增加库存，负数为减少库存", "修改库存", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /^-?\d+$/,
        inputErrorMessage: "请输入整数",
        inputValue: ""
      }).then(({ value }) => {
        let json = {
          stock: value,
          productId: this.dialogObj.id,
          id: val.id,
          operation: 2 // 2 修改
        };
        editStocktApi(json).then(() => {
          this.$message({
            type: "success",
            message: "库存修改成功"
          });
          this.initDialog();
        });
      });
    }
  },
  watch: {
    "dialogObj.show"(val) {
      if (val) {
        this.initDialog();
      }
    }
  }
};
</script>
<style scoped lang="scss"></style>
